﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XNASpelProjekt
{
    /// <summary>
    /// Base class for all bullets
    /// </summary>
    public abstract class BulletActor : Actor
    {
        #region Member Variables

        protected double angle;        //vinkeln som skottet färdas, 0 = rakt åt höger
        protected int velocity;        //hastigheten som skottet färdas i
        protected int damage;          //skadan skottet gör på sitt mål

        #endregion

        #region protected methods

        protected void mapCollisionCheck(Rectangle collision)
        {
            switch (CollisionDetection(collision))
            {
                case _CollisionType.noCollision:
                    X += (int)(Math.Cos(angle) * velocity);
                    Y += (int)-(Math.Sin(angle) * velocity);
                    break;
                default:
                    Game1.map.bulletsToRemove.Add(this);
                    break;
            }   
        }

        #endregion

        #region public methods

        /// <summary>
        /// Updates the bullet in the game.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            Rectangle collision;
            collision = Bounds;
            collision.X += (int)(Math.Cos(angle) * velocity);
            collision.Y += (int) -(Math.Sin(angle) * velocity);

            if (collision.Intersects(Game1.map.player.Bounds))
            {                
                int v;
                if ((angle > Math.PI / 2) && (angle < 3 * Math.PI / 2))
                {
                    v = -1;
                }
                else
                {
                    v = 1;
                }
                Game1.map.player.Damage(damage, v);
                Game1.map.bulletsToRemove.Add(this);
                return; 
            }

            mapCollisionCheck(collision);                 
        }

        #endregion

        #region constructors

        /// <summary>
        /// Creates a bullet object with specified texture, on specified position,
        /// and with a specified velocity and angle.
        /// </summary>
        /// <param name="texture">The bullets texture</param>
        /// <param name="x">The bullets x-position</param>
        /// <param name="y">The bullets y-position</param>
        /// <param name="damage">A bullets inflicted damage value</param>
        /// <param name="velocity">The bullets velocity</param>
        /// <param name="angle">The bullets traversed angle</param>
        public BulletActor(Texture2D texture, int x, int y, int damage = 0, int velocity = 0, double angle = 0) : base(texture, x, y, _Facing.right, false)
        {
            this.velocity = velocity;
            this.angle = angle;
            this.damage = damage;
        }

        #endregion
    }
}
